'''
@Company: TWL
@Author: xue jian
@Email: xuejian@kanzhun.com
@Date: 2020-04-09 19:26:54
'''
import sys
import numpy as np
from sklearn.metrics import roc_auc_score

dfm_inpath = '/data1/xuejian/sync/offline_train/rank_union/galaxy/result'
xgb_inpath = '/data1/xuejian/sync/offline_train/rank_union/xgboost/algorithm/xgb_preds_out_t'

dfm_list = []
xgb_list = []
all_label = []
with open(dfm_inpath, 'r') as f1, open(xgb_inpath, 'r') as f2:
    # for line in f1:
    for line in f2:
        # dfm_score = line.strip()
        dfm_score = f1.readline().strip()
        # f2_line = f2.readline().strip()
        f2_line = line.strip()
        # print(f2_line)
        xgb_score = f2_line.split('\t')[3]
        label = f2_line.split('\t')[4]
        dfm_list.append(float(dfm_score))
        xgb_list.append(float(xgb_score))
        all_label.append(int(label))

def cal_auc(label, pred):
    return roc_auc_score(np.asarray(label), np.asarray(pred))

print("dfm size: ", len(dfm_list))
print("xgb size: ", len(xgb_list))
print("label size: ", len(all_label))

df_weight = 0.0
i = 1
while i < 100:
    df_weight += 0.01
    xgb_weight = 1 - df_weight
    all_pred = []
    for j in range(0, len(all_label)):
        fusion_pred = df_weight * dfm_list[j] + xgb_weight * xgb_list[j]
        all_pred.append(fusion_pred)
    auc = cal_auc(np.asarray(all_label), np.asarray(all_pred))
    print("deepfm score weight: " + str(df_weight) + '\t' + "xgboost score weight: " + str(xgb_weight) + '\t' + "auc=", auc)
    i += 1